package com.demo.servlet;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Test
 */
@WebServlet("/switchLanguage")
public class MultiLanguageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MultiLanguageServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Locale locale = request.getLocale();
		checkLanguage(request);
		String lang = request.getParameter("lang");
		if (!"".equals(lang) && null != lang) {
			request.getSession().setAttribute("lang", lang);
			return;
		}
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	public static void checkLanguage(HttpServletRequest request) {
		Locale iLocale = request.getLocale();
		String country = iLocale.getCountry();
		String language = iLocale.getLanguage();
		System.out.println(">当前国家：" + country + "  >使用语言:" + language + "  >当前session："
				+ request.getSession().getAttribute("lang"));
		request.getSession().setAttribute("currentLang", language);
	}

}
